package com.qingpeng.pay.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.qingpeng.pay.R;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.beans.TeamBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.APP;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TeamActivity extends BaseActivity {

    @BindView(R.id.title)
    TitleBar title;
    @BindView(R.id.text_team)
    TextView textTeam;
    @BindView(R.id.text_invite)
    TextView textInvite;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_team;
    }

    @Override
    protected int getRootViewId() {
        return 0;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void onResume() {
        title.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title.getLlRight().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TeamActivity.this,CodeListActivity.class));
            }
        });
        Call<TeamBean> agentlist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).agentlist();
        agentlist.enqueue(new Callback<TeamBean>() {
            @Override
            public void onResponse(Call<TeamBean> call, Response<TeamBean> response) {
                TeamBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(TeamActivity.this, body.getInfo());
                    return;
                }
                textTeam.setText("总人数:"+body.getData().getTolnum()+"");
                textInvite.setText("可邀请:"+body.getData().getSurimsinum()+"");
            }

            @Override
            public void onFailure(Call<TeamBean> call, Throwable t) {
                ToastUtils.showToast(TeamActivity.this, "网络请求错误,请稍后重试");
            }
        });
        super.onResume();
    }

    @OnClick({R.id.ll_team, R.id.ll_invite})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_team:
                startActivity(new Intent(TeamActivity.this, TeamListActivity.class));
                break;
            case R.id.ll_invite:
                final AlertDialog.Builder builder = new AlertDialog.Builder(TeamActivity.this);
                builder.setTitle("刷宝")
                        .setMessage("确认生成邀请码")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Call<NullBean> createcode = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).createcode();
                                createcode.enqueue(new Callback<NullBean>() {
                                    @Override
                                    public void onResponse(Call<NullBean> call, final Response<NullBean> response) {
                                        if (response.body().getStatus() != 1) {
                                            ToastUtils.showToast(TeamActivity.this, response.body().getInfo());
                                            return;
                                        }
                                        startActivity(new Intent(TeamActivity.this, TextActivity.class)
                                                .putExtra("type","邀请").putExtra("string",response.body().getData().toString()));

                                    }
                                    @Override
                                    public void onFailure(Call<NullBean> call, Throwable t) {
                                        ToastUtils.showToast(TeamActivity.this, "网络请求错误,请稍后重试");
                                    }
                                });
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                builder.create().show();
                break;
        }
    }
}
